﻿//using System;
//using System.Collections.Generic;
//using System.Collections.ObjectModel;
//using System.Linq;
//using System.ServiceModel;
//using System.ServiceModel.Channels;
//using System.ServiceModel.Description;
//using System.ServiceModel.Dispatcher;
//using MugenInjection.Interface;
//using MugenInjection.Interface.Components;
//using MugenInjection.Parameters;
//using MugenInjection.Web;
//
//namespace MugenInjection.Wcf
//{
//    /// <summary> 
//    /// Service behavior implementation for MugenInjection.
//    /// </summary>
//    public class MugenInjectionServiceBehavior : IServiceBehavior
//    {
//        #region Implementation of IServiceBehavior
//
//        /// <summary>
//        /// Provides the ability to inspect the service host and the service description to confirm that the service can run successfully.
//        /// </summary>
//        /// <param name="serviceDescription">The service description.</param><param name="serviceHostBase">The service host that is currently being constructed.</param>
//        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
//        {
//        }
//
//        /// <summary>
//        /// Provides the ability to pass custom data to binding elements to support the contract implementation.
//        /// </summary>
//        /// <param name="serviceDescription">The service description of the service.</param><param name="serviceHostBase">The host of the service.</param><param name="endpoints">The service endpoints.</param><param name="bindingParameters">Custom objects to which binding elements have access.</param>
//        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
//                                         Collection<ServiceEndpoint> endpoints,
//                                         BindingParameterCollection bindingParameters)
//        {
//        }
//
//        /// <summary>
//        /// Provides the ability to change run-time property values or insert custom extension objects such as error handlers, message or parameter interceptors, security extensions, and other custom extension objects.
//        /// </summary>
//        /// <param name="serviceDescription">The service description.</param><param name="serviceHostBase">The host that is currently being built.</param>
//        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
//        {
//            var endpointDispatchers = serviceHostBase
//                .ChannelDispatchers
//                .OfType<ChannelDispatcher>()
//                .SelectMany(channelDispatcher => channelDispatcher.Endpoints);
//            IInjector injector = MugenInjectionHttpApplication.ApplicationConfiguration.Injector;
//            var behaviorManagerComponent = injector.Components.Get<IBehaviorManagerComponent>();
//            foreach (EndpointDispatcher endpointDispatcher in endpointDispatchers)
//            {
//                //Sets an InstanceProvider.
//                IInstanceProvider instanceProvider;
//                if (!injector.TryGet(out instanceProvider, WcfConfigExtension.InstanceProviderKey, new ConstructorParameter("serviceType", serviceDescription.ServiceType)))
//                    throw new InvalidOperationException("The main container is not registered IInstanceProvider.");
//                endpointDispatcher.DispatchRuntime.InstanceProvider = instanceProvider;
//
//                //Adding an internal inspectors.
//                var dispatchMessageInspectors = behaviorManagerComponent.GetAll<IDispatchMessageInspector>();
//                foreach (IDispatchMessageInspector dispatchMessageInspector in dispatchMessageInspectors)
//                    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(dispatchMessageInspector);
//
//                //Adding an user inspectors.
//                IEnumerable<IDispatchMessageInspector> result;
//                if (!injector.TryGetAll(out result, serviceDescription.ServiceType, WcfConfigExtension.MessageInspectorKey))
//                    continue;
//                foreach (IDispatchMessageInspector dispatchMessageInspector in result)
//                    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(dispatchMessageInspector);
//            }
//        }
//
//        #endregion
//    }
//}